package com.ophelisis.actions.homepage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.persistence.dao.LogoDAO;
import com.ophelisis.persistence.dao.MenuDAO;
import com.ophelisis.persistence.dao.MenuGroupDAO;
import com.ophelisis.persistence.dao.MenuImageDAO;
import com.ophelisis.persistence.domain.MenuGroupVO;
import com.ophelisis.persistence.domain.MenuImageVO;
import com.ophelisis.persistence.domain.MenuVO;

/** <pre>
 * Class : com.ophelisis.actions.homepage.Program / Program.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 01. 17.
 */
@Controller
public class Program {
	@Autowired
	MenuGroupDAO menuGroupDAO;
	
	@Autowired
	MenuDAO menuDAO;
	
	@Autowired
	LogoDAO logoDAO;
	
	@Autowired
	MenuImageDAO menuImageDAO;

	private List<MenuGroupVO> menuGroupList;
	private List<MenuVO> menuList;
	private String logoFileName;
	private List<MenuImageVO> menuImageList;
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "Homepage Log> ";
	
	@RequestMapping(value="program.action")
	public ModelAndView program(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="programMain") String sTarget,
			@RequestParam(value="_groupId", defaultValue="0") String sMenuGroupId,
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuNm", defaultValue="") String sMenuNm,
			@RequestParam(value="_prevCode", defaultValue="") String sPrevCode,
			@RequestParam(value="_startRowNo", defaultValue="0") String sStartRowNo,
			@RequestParam(value="_pageSize", defaultValue="15") String sPageSize,
			@RequestParam(value="searchVal1", defaultValue="") String sSearchVal1,
			@RequestParam(value="isNew", defaultValue="Y") String isNew) throws Exception {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession(false);
		
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " Program called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			
			if (sMenuGroupId.equals("0")) {
				MenuGroupVO menuGroupVO = new MenuGroupVO();
				
				params.put("menuGroupLink", "program.action");
				menuGroupVO = menuGroupDAO.getMenuGroupInfo(params);
				
				sMenuGroupId = menuGroupVO.getMenuGroupId();
				sMenuNm = menuGroupVO.getMenuGroupNm();
				params.clear();
			}
			
			// 공통 요소
			mav.addObject("_groupId", sMenuGroupId);
			mav.addObject("_menuId", sMenuId);
			mav.addObject("_menuNm", sMenuNm);
			mav.addObject("_prevCode", sPrevCode);
			mav.addObject("_startRowNo", sStartRowNo);
			mav.addObject("_pageSize", sPageSize);
			mav.addObject("searchVal1", sSearchVal1);
			
			int iStartRowNo = Integer.parseInt(sStartRowNo); // 해당 페이지의 시작 번호
			int iPageSize = Integer.parseInt(sPageSize); // 한 페이지에 보일 레코드 수

			params.put("startRowNo", iStartRowNo); // 공통 코드 그룹 ID (1:메뉴 유형)
			params.put("pageSize", iPageSize);
			params.put("searchVal1", sSearchVal1);
			params.put("userId", sPrevCode);
			params.put("updateUser", (String)session.getAttribute("userId"));
			
			if (sTarget.equals("programMain")) {
				params.put("useYn", "Y");
				params.put("menuGroupId", sMenuGroupId);
				
				mav = returnMgmt("main", mav, params);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			menuGroupList = menuGroupDAO.getMenuGroupList();
			menuList = menuDAO.getMenuList();
			logoFileName = logoDAO.getLogoList(params).get(0).getLogoFileNew();
			menuImageList = menuImageDAO.getMenuImageDetail(params);

			mav.addObject("homeMenuGroupList", menuGroupList);
			mav.addObject("homeMenuList", menuList);
			mav.addObject("logoFileName", logoFileName);
			mav.addObject("menuImageList", menuImageList);
			
			int menuImageCount = 0;
			if (menuImageList != null && menuImageList.size() > 0) {
				menuImageCount = menuImageList.get(0).getImageFileNew().split("\\|").length;
			}

			mav.addObject("menuImageCount", menuImageCount);
			
			mav.setViewName("/WEB-INF/jsp/homepage/program.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " Program Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
}

